package com.stx.friends.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.stx.friends.dao.RelationDao;
import com.stx.friends.entity.RelationEntity;
import com.stx.friends.vo.RelationVo;

public class RelationDaoImpl extends JdbcDaoSupport implements RelationDao{

	/**
	 * @param args
	 */
	public List<RelationEntity> list (RelationVo vo){
		
		String sql = "select * from wrx_relation";
		
		JdbcTemplate jt = getJdbcTemplate();
		
		List<RelationEntity> relationList = jt.query(sql, new RelationRowMapper());

		return relationList;
	}
	
	class RelationRowMapper implements RowMapper {
		public Object mapRow(ResultSet rs, int rowNum)throws SQLException {

			RelationEntity entity = new RelationEntity();
			
			entity.setwrxUid(rs.getString("wrxUid"));
			entity.setWrxUid(rs.getString("wrxUid"));
			entity.setFriendId(rs.getString("friendId"));
			entity.setCreateTime(rs.getString("createTime"));
			entity.setDelFlag(rs.getString("delFlag"));
			
			return entity;
		}
	
	}

	@Override
	public void delRelation(String wrxUid) {
		// TODO Auto-generated method stub
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "delete from wrx_relation where wrxUid = ?";

		// 构建参数数组
		Object[] args = { wrxUid };

		jt.update(sql, args);

	}

	@Override
	public void addRelation(RelationVo relation) {
		// TODO Auto-generated method stub
		JdbcTemplate jt = getJdbcTemplate();

		String sql = "insert into wrx_relation(wrxUid,wrxUid,friendId,createTime,delFlag) values(seq_wrx_relation.nextval,?,?,?)";

		// 构建参数数组
		Object[] args = { relation.getWrxUid(), relation.getwrxUid(), relation.getFriendId(), relation.getDelFlag(), relation.getCreateTime() };
		
		jt.update(sql, args);

	}
}
